# coding=utf-8
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from backstage.common.utils import view_funcs


class ImageUpload(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAdminUser,)

    def post(self, request):
        """
        上传图片
        """
        file = request.FILES.get("file")
        if not file:
            return Response({
                "error": "文件缺失",
            })

        url = view_funcs.upload_file(file)
        return Response({
            "status": 200,
            "msg": "success",
            "data": {
                "img_url": url,
            }
        })
